package cn.bouncyslime.huffmanTree.util;

import java.io.*;

/**
 * 用来读取待进行编码的文档，返回存储文档内容的字符串
 * 作者： 张一卓
 * 时间： 2019/12/31 11:14
 */
public class TextReader {
    /**
     * 读取带编码文档
     *
     * @param filePath 文档路径
     * @return 存储文档内容的字符串
     */
    public static String readText(String filePath) {
        StringBuilder stringBuilder = new StringBuilder();
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                throw new FileNotFoundException("没有找到文件");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            int character;
            while ((character = reader.read()) != -1) {
                stringBuilder.append((char) character);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
